import socket
import struct
import sys

class AhoPack():

    aho_struct =  struct.Struct("!BH")

    def __init__(self,data,op_code=0):
        self.op_code = op_code
        self.data =  data.encode()
        self.lenght = len(self.data) + 3


    def pack(self):

        return  self.aho_struct.pack(self.op_code,self.lenght) + self.data  


if len(sys.argv) < 3:
    print("Need Args")
    sys.exit(0)
address = (sys.argv[1],int(sys.argv[2]))
while True:
    str_data = input("TOBESEND:")
    op_code = input("OP_CODE:")
    op_code = int(op_code,10)
    peer = socket.socket()
    peer.connect(address)
    aho = AhoPack(str_data,op_code=op_code)
    print(aho.pack())
    peer.send(aho.pack())
    peer.close()



